package com.jfinal.aceadmin.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class StringUtil {

	private static long MOMENT_TIME = 20 * 60 * 1000;
	private static long HOUR_TIME = 60 * 60 * 1000;
	private static long MINUTE_TIME = 60 * 1000;
	private static long DAY_TIME = 24 * 60 * 60 * 1000;

	public static List<String> spiltString2List(String data) {
		List<String> result = new ArrayList<String>();
		if (!StringUtils.isBlank(data)) {
			String[] arr = data.split(",");
			result = Arrays.asList(arr);
		}
		return result;
	}

	public static String getTimeTemplate(Date date) {
		long recentTime = DateUtil.getMistiming(date);
		System.out.println("recentTime : " + recentTime);
		String timeTemplate = null;
		if (recentTime / MOMENT_TIME < 1) {
			timeTemplate = "a moment ago";
		} else if (recentTime / HOUR_TIME < 1) {
			timeTemplate = recentTime / MINUTE_TIME + " minutes ago";
		} else if (recentTime / DAY_TIME < 1) {
			timeTemplate = DateUtil.getAmPmDate(date);
		} else if (recentTime / DAY_TIME < 2) {
			timeTemplate = "1 day ago";
		} else {
			timeTemplate = recentTime / DAY_TIME + " days ago";
		}
		return timeTemplate;
	}

	public static String getFormatSize(double size) {
		double kiloByte = size / 1024;
		if (kiloByte < 1) {
			return size + "Byte(s)";
		}

		double megaByte = kiloByte / 1024;
		if (megaByte < 1) {
			BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
			return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + "KB";
		}

		double gigaByte = megaByte / 1024;
		if (gigaByte < 1) {
			BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
			return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + "MB";
		}

		double teraBytes = gigaByte / 1024;
		if (teraBytes < 1) {
			BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
			return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + "GB";
		}
		BigDecimal result4 = new BigDecimal(teraBytes);
		return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
				+ "TB";
	}

}
